package org.i_plus.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.model.ConstellationBean;
import org.i_plus.model.services.ConstellationService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;

public class ConstellationServlet extends HttpServlet{
	private ConstellationService service;
	@Override
	public void init() throws ServletException {
		ServletContext application = this.getServletContext();
		ApplicationContext context=
				WebApplicationContextUtils.getWebApplicationContext(application);
		this.service = (ConstellationService)context.getBean("constellationService");
		super.init();
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	
		List<ConstellationBean> list = service.selectAll();
		
		Gson gson = new Gson();
		String result=gson.toJson(list);
		
		resp.setCharacterEncoding("UTF-8");
		resp.getWriter().print(result);
		return;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}



	
	
}
